﻿var Normal_KeywordList;
var Normal_KeywordStore;
// 功能模块grid展示
Normal_KeywordList = function () {
    var url = "/manager/Normal_Keyword/List";
    var sm = Ext.create('Ext.selection.CheckboxModel');

    //grid 填充
    Ext.define('Normal_KeywordGridModel', {
        extend: 'Ext.data.Model',
        fields: [
            { name: "Id", type: "string" },
            { name: "Keyword", type: "string" },
            { name: "SearchNum", type: "string" },
            { name: "IsHot", type: "string" }
            ]
    });

    //grid获取 json数组
    Normal_KeywordStore = Ext.create('Ext.data.Store', {
        pageSize: CMN.pageSize,
        model: 'Normal_KeywordGridModel',
        buffered: false,  //是否动态加载
        proxy: {
            actionMethods: 'post',
            type: 'ajax',
            url: url,
            reader: {
                type: 'json',
                root: 'root',
                totalProperty: 'totalPorperty'
            }
        },
        sorters: [{
            property: 'Id',
            direction: 'DESC'
        }],

    });

    //分页设置
    var pagingBar = Ext.create('Ext.PagingToolbar', {
        displayInfo: true, emptyMsg: "没有数据显示", displayMsg: "显示从{0}条数据到{1}条数据，共{2}条数据",
        store: Normal_KeywordStore,
        pageSize: CMN.pageSize,
        //显示右下角信息
        prevText: "上一页",
        nextText: "下一页",
        refreshText: "刷新",
        lastText: "最后页",
        firstText: "第一页",
        beforePageText: "当前页",
        afterPageText: "共{0}页"
    });

    //删除选中行信息
    DeleteNormal_KeywordFn = function () {
        var row = Ext.getCmp("Normal_KeywordPanel").getSelectionModel().getSelection();
        if (row.length == 0) {
            Ext.Msg.alert("提示信息", "请您至少选择一个!");
        }
        else if (row.length > 1) {
            var idList = "";
            for (var i = 0; i < row.length; i++) {
                if (i == 0) {
                    idList = row[0].data.Id;
                }
                else {
                    idList = idList + "_" + row[i].data.Id;
                }
            }
            //Ext.Msg.alert("提示信息", "对不起只能选择一个!");
            Ext.Msg.confirm("提 示!", "您确定要删除" + idList + "吗?", function (btn) {
                if (btn == "yes") {
                    Ext.Ajax.request({
                        url: "/manager/Normal_Keyword/DeleteList/" + idList,
                        method: "get",
                        success: function (d) {
                            //成功后
                            var action = Ext.JSON.decode(d.responseText);
                            //alert(action.success);
                            var flag = action.success;
                            if (flag == "true") {
                                Normal_KeywordStore.loadPage(1);
                                Ext.MessageBox.alert("恭喜", "删除成功!");
                            }
                            else {
                                var message = action.result.message;
                                if (message) {
                                    Ext.MessageBox.alert("删除失败!", message);
                                }
                                else {
                                    Ext.MessageBox.alert("提示!", "删除失败!");
                                }
                            }
                        },
                        failure: function (form, action) {
                            Ext.MessageBox.alert("提示!", "修改失败!");
                        }
                    })
                }
                else {

                }
            })
        }
        else if (row.length == 1) {
            Ext.Msg.confirm("提 示!", "您确定要删除" + row[0].data.Id + "吗?", function (btn) {
                if (btn == "yes") {
                    Ext.Ajax.request({
                        url: "/manager/Normal_Keyword/Delete/" + row[0].data.Id,
                        method: "POST",
                        success: function (d) {
                            //成功后
                            var action = Ext.JSON.decode(d.responseText);
                            //alert(action.success);
                            var flag = action.success;
                            if (flag == "true") {
                                Normal_KeywordStore.loadPage(1);
                                Ext.MessageBox.alert("恭喜", "删除成功!");
                            }
                            else {
                                var message = action.result.message;
                                if (message) {
                                    Ext.MessageBox.alert("删除失败!", message);
                                }
                                else {
                                    Ext.MessageBox.alert("提示!", "删除失败!");
                                }
                            }
                        },
                        failure: function (form, action) {
                            Ext.MessageBox.alert("提示!", "修改失败!");
                        }
                    })
                }
                else {

                }
            })
        }
    }


    //修改选中行信息
    EditNormal_KeywordFn = function () {
        var row = Ext.getCmp("Normal_KeywordPanel").getSelectionModel().getSelection();
        if (row.length == 0) {
            Ext.Msg.alert("提示信息", "您没有选中任何行!");
        }
        else if (row.length > 1) {
            Ext.Msg.alert("提示信息", "对不起只能选择一个!");
        } else if (row.length == 1) {
            EditNormal_Keyword(row[0].data.Id);
        }

    }

    //新增信息
    SaveNormal_KeywordFn = function () {
        InsertNormal_Keyword();
    }


    //新增信息
    SearchNormal_KeywordFn = function () {
        Normal_KeywordStore.load({
            params: {
                searchword: Ext.get('searchword-inputEl').dom.value
                
            }
        });
       
    }

    //grid配置
    var grid = Ext.create('Ext.grid.Panel', {
        id: "Normal_KeywordPanel",
        renderTo: "Normal_KeywordGrid",
        layout: "fit",
        alias: 'widget.methodPanel',
        columnLines: true,
        loadMask: true,
        frame: true,
        border: false,
        selModel: sm,   //选择框
        autoScroll: true,
        store: Normal_KeywordStore,
        ds: Normal_KeywordStore,
        sm: sm,
        columnLines: true,
        //cm: cm2,
        viewConfig: {
            stripeRows: true,
            trackOver: true,
            emptyText: '<div style="color:#999;margin:5px;">当前没有记录显示<div>'
        },
        stripeRows: true,
        listeners: {
            "itemdblclick": function (g, rowIndex, e) {
                EditNormal_KeywordFn();
            }
        },
        bbar: pagingBar,
        tbar: [            		 //工具条设置
            		 {
            		 text: "新增",
            		 tooltip: "新增",
            		 iconCls: "addicon",
            		 handler: SaveNormal_KeywordFn
           }, "", "-", "", {
               text: "编辑",
               tooltip: "编辑",
               iconCls: "editicon",
               handler: EditNormal_KeywordFn
           }, "", "-", "", {
               text: "删除",
               tooltip: "删除",
               iconCls: "deleteicon",
               handler: DeleteNormal_KeywordFn
           }, "", "-", "", {
               xtype: 'textfield',
               fieldLabel: '用户名',
               id: 'searchword',
               name: 'searchword',
               anchor: '90%'
           }, {
               text: "搜索",
               tooltip: "搜索",
               iconCls: "editicon",
               handler: SearchNormal_KeywordFn
           }, "-"],
        columns: [
               Ext.create('Ext.grid.RowNumberer'),

                { header: "递增Id", dataIndex: "Id", width: 80, sortable: true },

                { header: "分词", dataIndex: "Keyword", width: 200, sortable: true },

                { header: "搜索次数", dataIndex: "SearchNum", width: 80, sortable: true },

                { header: "是否热门关键词", dataIndex: "IsHot", width: 80, renderer: function (value) { if (value == "1") { return "是"; } else { return "否"; } }, sortable: true },
                { header: "删除", dataIndex: "Id", renderer: function (value) { return "<a href='javascript:void(0);' title='删除' onclick='DeleteNormal_KeywordFn()'class='deleteBtn' >&nbsp;&nbsp;&nbsp;</a>" }, width: 40 }
            ]
    });




       Normal_KeywordStore.on('beforeload', function (s,o) {
            
           s.proxy.extraParams['searchword']= Ext.get('searchword-inputEl').dom.value
           
       
        });
    Normal_KeywordStore.loadPage(1);
  
}
    Normal_KeywordList();
